<template>

  <div class="L-base-list">
    <!-- <van-sticky> -->
      <div class="L-base-search">
        <van-search v-model="keyword" show-action placeholder="请输入搜索关键词" >
        <template #action>
          <span @click.stop="handleSearch">搜索</span>
          <span @click="handleRefCancle()" style="margin-left:15px">取消</span>
        </template>
      </van-search>
    
      </div>
      
    <!-- 列表 -->
     <div style="flex-grow: 1;flex-shrink: 1;height: 90vh;overflow: scroll;">
      <van-list  v-model:loading="isLoading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-checkbox-group v-model="selecteddata" ref="checkboxGroup">
        <div class="base-item" v-for="(item, index) in list" :key="index">
          <RefItem :item="item" :mode="refMode" :handleRefClick="handleRefClick" @click="handleClick(item)"></RefItem>
        </div>
      </van-checkbox-group>

    </van-list>
     </div>
    
  </div>
  <van-tabbar safe-area-inset-bottom v-if="refMode === 'multi'">
    <div class="L-orgRef-tabbar">
      <van-button color="#1989fb" type="primary" style="width:90%" @click="handleConfirm">确定</van-button>
    </div>
  </van-tabbar>

</template>

<script setup lang="ts">
import { closeToast, showLoadingToast } from "vant";

// import BaseSearch from "../BaseSearch.vue";
import RefItem from "./RefItem.vue";
import { apiRefListNew } from "@/request/api/api";
import { useRouter, useRoute } from "vue-router";
import {onMounted,ref,defineProps,defineEmits,watch,onUnmounted,inject,} from "vue";
const router = useRouter();
const route = useRoute();

const list = ref([]);
const page = ref(1);
const pagecount = ref(50); //每页条数
const firstLoad = ref(true); //是否第一次加载
const isLoading = ref(false); //是否加载中
const finished = ref(false); //列表是否加载完成
const keyword = ref("");


const selecteddata = ref([]);
function handleConfirm() {
  let id = "";
  let title = "";
  let address = "";
  let idaccount_ref = ""

  for (let i = 0; i < selecteddata.value.length; i++) {
    const item = selecteddata.value[i];
    id += `${item.id};`;
    title += `${item.sname};`;
    address += `${item?.saddress};`;
    idaccount_ref += `${item?.idaccount_ref};`

  }
  const returnObj = {
    title,
    id,
    address,
    idaccount_ref
  };
  props.handleRefClick(returnObj);
}
onMounted(() => {
  // console.log("ref is onMounted")
  // onLoad()
})

const props = defineProps({
  refParams: {
    type: Object,
    default: {},
  },
  handleRefClick: {
    type: Function,
    default: () => { },
  },
  handleRefCancle: {
    type: Function,
    default: () => { },
  },
  refMode: {
    type: String,
    default: "normal",
  },
  refObject: {
    type: [String, Array],
    default: "",
  },
});

function onLoad() {

  // if(finished.value){
  //   return 
  // }
 
  const loadParams = props.refParams;
  console.log("props.refParams",props.refParams)

  let data = {
    ref_object: loadParams.object,
    ref_toobject: loadParams.toobject,
    ref_tofieldas: loadParams.field,
    ref_inview: loadParams.inview || "",
    view_page: page.value,
    view_id: loadParams.id || loadParams.sRefKey || loadParams.viewId,
    ref_classname: loadParams.classname,
    ref_params: loadParams.params,
    ref_conditionval: keyword.value,
  };

  showLoadingToast({
    message: "加载中...",
    forbidClick: true,
    duration: 0,
  });

  isLoading.value = true;
  apiRefListNew(data).then((res) => {
    if (res.code === 0) {

      if (res.data?.jsondata?.outdata && Array.isArray(res.data?.jsondata?.outdata) && res.data?.jsondata?.outdata?.length>0) {
        list.value = [...list.value, ...res.data?.jsondata?.outdata];
        page.value = page.value + 1;
        isLoading.value = false;
        closeToast();

        
      } else {
        closeToast();
        finished.value = true;
        isLoading.value = false;
        // list.value = [...list.value, ...res.data?.jsondata?.outdata];



        
      }

    }

  }).finally(() => {

  });
}

//字段映射hooks
import { useFieldMap } from "@/hooks/hooks.js";
const { getMapInfo } = useFieldMap();


interface returnObjInterface {
  id: string,
  title: string,
  [key: string]: string

}
async function handleClick(item) {
  const { refMode, refParams, handleRefClick } = props
  console.log(item,refParams,334443333444)
  if (refMode === "normal") {


    //字段映射
    const data = {
      ref_object: refParams.object,
      ref_id: item.id,
      ref_toobject: refParams.toobject,
      ref_tofieldas: refParams.field,
      ref_tofield: refParams.field,
      ref_classname: refParams.classname,
      ref_params: refParams.params,
    };

    const mapRes = await getMapInfo(data);
    const outdata = mapRes.data.jsondata.outdata;
    const returnObj: returnObjInterface = {
      id: item.id,
      title: outdata[`${refParams['field']}_ref`]
      
    };
    if (Object.getOwnPropertyNames(outdata).length > 2) {
      returnObj.fieldmap = outdata;
      returnObj.mapobject = refParams.toobject;
    }

        console.log(returnObj,6767)


    props.handleRefClick(returnObj);
  }
}

//初始化查询条件
function initCondition() {
  page.value = 1;
  finished.value = false;
  list.value = [];
}

function handleSearch() {
  initCondition();
  onLoad();
}
</script>

<style scoped></style>